<?php
require_once realpath(dirname(__FILE__).'/../../app/init.php');
require_once 'AIR2_DBManager.php';

AIR2_DBManager::init();
$conn = AIR2_DBManager::get_master_connection();

/**
 * Offloads data in the database to files (which can be loaded as fixtures later)
 */
echo "********************************************************************************\n";
echo "*******        OFFLOAD DATA                                           **********\n";
echo "********************************************************************************\n";
echo "    This will offload all data in doctrine tables into fixtures files\n";
echo "    (the database will not be altered during this process)\n";
echo "    Data will be stored in 'app/fixtures/offload/'\n";

$models = Doctrine::getLoadedModels();
echo "    There are currently ".count($models)." loaded models.\n";
echo "    Offload data for these tables? (yes/no)\n    >";
$answer = strtoupper(trim(fgets(STDIN)));


if ($answer === 'YES' || $answer === 'Y') {
    foreach($models as $modname) {
        Doctrine::getTable($modname)->setConnection($conn);
    }


    try {
        Doctrine::dumpData(APPPATH.'/offload/', true);
        echo "    Data offloaded successfully!\n";
    } catch (Doctrine_Exception $e) {
        echo "    ERROR: ".$e->getMessage()."\n";
    }
} else {
    echo "    No data has been offloaded\n";
}

echo "    exiting...\n\n";

?>
